<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/3
 * Time: 18:35
 */

namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\Exception;

class Custom extends Controller
{
    //定制需求显示
    public function index(){
        $table='custom';
        $where['id']=['>',0];
        if($this->request->isPost()){
            $post=$this->request->post();
            if(!empty($post['username'])){
                $where['username']=['like','%'.$post['username'].'%'];
            }
            if(!empty($post['phone'])){
                $where['phone']=['like','%'.$post['phone'].'%'];
            }
            if(!empty($post['status'])){
                $where['status']=$post['status'];
            }
        }else{
            $post=$this->request->get();
            if(!empty($post['username'])){
                $where['username']=['like','%'.$post['username'].'%'];
            }
            if(!empty($post['phone'])){
                $where['phone']=['like','%'.$post['phone'].'%'];
            }
            if(!empty($post['status'])){
                $where['status']=$post['status'];
            }
        }
        $data=Db::name($table)->where($where)->field('id,username,phoneCode,phone,remarks,status,classId,tagName,createtime')->order('createtime desc')
            ->paginate(20,false,['query'=>request()->param()]);
        $this->assign('data',$data);
        return $this->fetch();
    }

    //查看详情
    public function update(){
        $id=input('id');
        $data=Db::name('custom')->where(['id'=>$id])->find();
        $data['service']=json_decode($data['service']);
        $this->assign('data',$data);
        return $this->fetch();
    }


    //定制服务配置
    public function config(){

        $table='custom_config';
        $datalit=[];

        $res=Db::name($table)->where(['type'=>1])->select();
        if(!empty($res)){
            foreach ($res as $k){
                $re_data['title']=$k['title'];
                $re_data['id']=$k['id'];
                $re_data['customtype']=$k['customtype'];
                $re_list=[];

                $reslist=Db::name($table)->where(['pid'=>$k['id']])->select();
                if (!empty($reslist)){
                    foreach ($reslist as $v){
                        $re_list[]=$v['title'];
                    }
                }
                $re_data['answer']=$re_list;
                array_push($datalit,$re_data);
            }
        }
        $this->assign('data',$datalit);
        return $this->fetch('customconfig');
    }

    //添加服务
    public function addconfig(){
        $table='custom_config';
        //执行提交
        if($this->request->isPost()){
           $data=$this->request->post();

            if(empty($data['title'])){
                return ['msg'=>'服务名不能为空','code'=>2];
            }
           if($data['id'] == 0){
               //新增
               Db::startTrans();
               try{
                   Db::name($table)->insert(['title'=>$data['title'],'customtype'=>$data['customtype'],'type'=>1,'pid'=>0]);
                   $id=Db::getLastInsID();
                   if(isset($data['data'])){
                       $data_son=[];
                       foreach ($data['data'] as $k){
                           $son=[];
                           $son['title']=$k['person_name'];
                           $son['type']=2;
                           $son['pid']=$id;
                           $data_son[]=$son;
                       }
                       Db::name($table)->insertAll($data_son);
                   }
                   Db::commit();
                   return ['msg'=>'添加成功','code'=>1];
               }catch (Exception $e){

                   Db::rollback();
                   return ['msg'=>'添加失败','code'=>2];
               }
           }else{
               //修改
               Db::name($table)->where(['id'=>$data['id']])->update(['title'=>$data['title'],'customtype'=>$data['customtype']]);

               //每条进行判断较为繁琐 ，先定为全部删除，然后重新添加
               Db::name($table)->where(['pid'=>$data['id']])->delete();
               $data_add=[];
               if(isset($data['data'])){
                   foreach ($data['data'] as $v){
                       $temp=[
                           'title'=>$v['person_name'],
                           'type'=>2,
                           'pid'=>$data['id'],
                       ];
                       $data_add[]=$temp;
                   }
                   Db::name($table)->insertAll($data_add);
               }

//               if(isset($data['data'])){
//                   //如果有设置选项 判断选项是否增删改
//                   // 获取修改前的选项做对比
//                   $res=Db::name($table)->where(['pid'=>$data['id']])->select();
//                   $set_arr=[];  //获取之前的id
//                   foreach ($res as $k){
//                       array_push($set_arr,$k['id']);
//                   }
//
//                   $data_add=[]; // 新增的数据
//                   $data_up=[]; //修改的数据
//                   //循坏修改后的数据
//                   foreach ($data['data'] as $v){
//                       if(isset($v['person_id'])){
//
//                       }
//                   }
//               }else{
//                   Db::name($table)->where(['pid'=>$data['id']])->delete();
//               }
               return ['msg'=>'修改成功','code'=>1];
           }
        }


        //执行访问
        $id=$this->request->get('id');
        if(empty($id)){
            $data=[];
        }else{
            $res=Db::name($table)->where(['id'=>$id])->find();
            $reslist=Db::name($table)->where(['pid'=>$id])->select();
            $data['id']=$id;
            $data['title']=$res['title'];
            $data['customtype']=$res['customtype'];
            $data['son']=$reslist;
        }
        $this->assign('data',$data);
        return $this->fetch();
    }

    //删除
    public function delconfig(){
        $id=$this->request->get('id');
        if(empty($id)){
            return ['msg'=>'删除失败，请刷新后再次尝试','code'=>2];
        }
        Db::startTrans();
        try{
            Db::name('custom_config')->where(['id'=>$id])->delete();
            Db::name('custom_config')->where(['pid'=>$id])->delete();
            Db::commit();
            return ['msg'=>'删除成功','code'=>1];
        }catch (\Exception $e){
            Db::rollback();
            return ['msg'=>'删除失败，请刷新后再次尝试','code'=>2];
        }
    }

    //定制完成
    public function complete(){
        $id=$this->request->get('id');
        if(empty($id)){
            return ['msg'=>'修改失败，请刷新后再次尝试','code'=>2];
        }

        Db::startTrans();
        try{
            Db::name('custom')->where(['id'=>$id])->update(['status'=>2]);
            Db::commit();
            return ['msg'=>'修改成功','code'=>1];
        }catch (\Exception $e){
            Db::rollback();
            return ['msg'=>'修改失败，请刷新后再次尝试','code'=>2];
        }
    }


}